import request from './request'

// 病友圈数据接口
export interface SickCircleItem {
  sickCircleId: number;
  userId: number;
  title: string;
  detail: string;
  releaseTime: number;
  collectionNum: number;
  commentNum: number;
  amount: number;
  collectionFlag?: number; // 添加收藏状态字段
}

// 病友圈列表响应接口
export interface SickCircleListResponse {
  result: SickCircleItem[];
  total?: number;
  page?: number;
  count?: number;
}

// 获取病友圈列表
export const getSickCircleList = async (page: number = 1, count: number = 3): Promise<SickCircleListResponse> => {
  try {
    // 检查用户是否已登录
    const sessionId = localStorage.getItem('sessionId') || localStorage.getItem('token');
    const userId = localStorage.getItem('userId');
    
    if (!sessionId || !userId) {
      throw new Error('用户未登录');
    }
    
    const response = await request.get('/health/user/sickCircle/v1/findSickCircleList', {
      params: { page, count }
    });
    
    if (response.status === 200) {
      return response.data;
    } else {
      throw new Error(`API返回状态码: ${response.status}`);
    }
  } catch (error) {
    console.error('获取病友圈列表失败:', error);
    throw error;
  }
};

// 病友圈详情接口参数
export interface SickCircleDetailParams {
  sickCircleId: number
}

// 病友圈详情响应数据
export interface SickCircleDetailResponse {
  status: string
  message: string
  result: {
    sickCircleId: number
    authorUserId: number
    departmentId: number
    adoptFlag: number
    title: string
    department: string
    disease: string
    detail: string
    treatmentHospital: string
    treatmentStartTime: string
    treatmentEndTime: string
    treatmentProcess: string
    picture: string
    collectionFlag: number
    collectionNum: number
    commentNum: number
    adoptNickName: string
    adoptHeadPic: string
    adoptComment: string
    adoptTime: number
  }
}

/**
 * 查询病友圈详情
 * @param params 病友圈ID参数
 * @returns Promise<SickCircleDetailResponse>
 */
export const getSickCircleDetail = (params: SickCircleDetailParams) => {
  return request.get<SickCircleDetailResponse>('/health/user/sickCircle/v1/findSickCircleInfo', {
    params
  }).then(response => response.data)
}

// 收藏病友圈接口参数
export interface AddCollectionParams {
  sickCircleId: number
}

// 收藏病友圈响应
export interface CollectionResponse {
  status: string
  message: string
}

/**
 * 收藏病友圈
 * @param params 病友圈ID参数
 * @returns Promise<CollectionResponse>
 */
export const addSickCircleCollection = (params: AddCollectionParams) => {
  return request.post<CollectionResponse>('/health/user/verify/v1/addUserSickCollection', null, {
    params
  }).then(response => response.data)
}

/**
 * 取消收藏病友圈
 * @param params 病友圈ID参数
 * @returns Promise<CollectionResponse>
 */
export const cancelSickCircleCollection = (params: AddCollectionParams) => {
  return request.delete<CollectionResponse>('/health/user/verify/v1/cancelSickCollection', {
    params
  }).then(response => response.data)
}


